package com.yonyou.pmclouds.basecom.util;

import com.yonyou.pmclouds.picture.entity.PictureViewVO;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public abstract class AbstractStorageUtil {

    private final static long EXPIRE_TIME = 24*60*60;

    public final PictureViewVO[] signForAppPics(String[] pics, HttpServletRequest request){
        List<PictureViewVO> viewList = new ArrayList<>();

        if (ArrayUtils.isEmpty(pics))
            return new PictureViewVO[0];
        for (String pic : pics) {
            if(StringUtils.isEmpty(pic))
                continue;
            PictureViewVO viewVO = new PictureViewVO();
            viewVO.setName(pic);
            viewVO.setType(checkVideo(pic)? "video" : "image");
            viewVO.setOrigUrl(signForOrig(pic, request));
            viewVO.setSnapshotUrl(signForSnapshot(pic, request));
            viewList.add(viewVO);
        }
        return viewList.toArray(new PictureViewVO[0]);
    }


    /**
     * 获取缩略图或首帧图地址
     *
     * @param picName
     * @return
     */
    private String signForSnapshot(String picName, HttpServletRequest request){
        String process = "";
        if(picName.indexOf(".mp4") != -1)
            return signUrlForSingleAppPic(picName,"video/snapshot,t_100,f_jpg,ar_auto,w_76,h_76", request);
        return signUrlForSingleAppPic(picName,"image/resize,m_fixed,w_76,h_76", request);
    }

    /**
     * 获得原图地址
     *
     * @param picName
     * @return
     */
    private String signForOrig(String picName, HttpServletRequest request){
        return signUrlForSingleAppPic(picName,null, request);
    }

    /**
     * 检查是否是视频格式
     *
     * @param picName
     * @return
     */
    private boolean checkVideo(String picName){
        return -1 != picName.indexOf(".mp4") ;
    }

    /**
     * build server info for upload
     * @param dir
     * @return
     * @throws UnsupportedEncodingException
     */
    public abstract Map<String, String> getSignWithDir(String dir, HttpServletRequest request);

    /**
     *
     * @param pic
     * @return
     */
    public abstract String signUrlForSingleAppPic(String pic, String process, HttpServletRequest request);

    /**
     *
     * @param picturePaths Array of the picture path to be applied for url
     * @param process Process parameters to be applied for process picture, like get picture thumbnail
     * @param request HTTP request
     * @return
     */
    public final String[] signUrlForMultiPictures(String[] picturePaths, String process, HttpServletRequest request){
        if(picturePaths == null)
            return null;

        String []  urls = new String[picturePaths.length];
        int i = 0;
        for(String path : picturePaths){
            urls[i++] = this.signUrlForSingleAppPic(path, process, request);
        }

        return urls;
    }

    public abstract long getFileContentLength(String fileNameKey);
}
